﻿using Deployment.Data.Deploy;
using Deployment.Data.Deploy.Steps;
using Deployment.Data.UI.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Xceed.Wpf.Toolkit;

namespace Deployment.Data.UI.Controls.Step
{
    /// <summary>
    /// Interaction logic for StepEditorControl.xaml
    /// </summary>
    public partial class StepEditorControl : AbstractDialogContentControl
    {
        public static readonly DependencyProperty DeploymentProperty =
            DependencyProperty.Register("Deployment", typeof(DeploymentFile), typeof(StepEditorControl), null);

        public static readonly DependencyProperty EnvironmentsProperty =
            DependencyProperty.Register("Environments",
                typeof(Data.Environment.EnvironmentsFile),
                typeof(StepEditorControl));

        public event DependencyPropertyChangedEventHandler PropertyChanged;

        private void SetValueDp(DependencyProperty property, object value)
        {
            SetValue(property, value);
            if (PropertyChanged != null)
                PropertyChanged(this, new DependencyPropertyChangedEventArgs());
        }

        public DeploymentFile Deployment
        {
            get { return (DeploymentFile)GetValue(DeploymentProperty); }
            set 
            { 
                SetValueDp(DeploymentProperty, value);
                //Update the stepId of the current step to try and prevent duplicates.
                if (this.DataContext != null && this.DataContext is Deployment.Data.Deploy.Steps.Step && (this.DataContext as Deployment.Data.Deploy.Steps.Step).Sequence == 0)
                {
                    int maxSequence = 0;
                    if (Deployment != null)
                    {
                        foreach (Deployment.Data.Deploy.Steps.Step step in Deployment.Steps)
                        {
                            if (maxSequence < step.Sequence)
                                maxSequence = step.Sequence;
                        }
                    }
                    (this.DataContext as Deployment.Data.Deploy.Steps.Step).Sequence = maxSequence+1;
                }
            }
        }

        public Data.Environment.EnvironmentsFile Environments
        {
            get { return (Data.Environment.EnvironmentsFile)GetValue(EnvironmentsProperty); }
            set 
            { 
                SetValueDp(EnvironmentsProperty, value);
                this.EditableTaskList.Environments = Environments;
            }
        }

        public StepEditorControl(Deployment.Data.Deploy.Steps.Step step=null)
        {
            InitializeComponent();
            if (step == null)
                this.DataContext = new Deployment.Data.Deploy.Steps.Step();
            else
                this.DataContext = step;
        }

        private void TaskList_DoubleClick(object sender, MouseButtonEventArgs e)
        {
            var item = ((FrameworkElement)e.OriginalSource).DataContext as Deployment.Data.Deploy.Steps.Step;
            if (item != null)
            {
                var CloneObj = item.Clone();
                //TODO perform Task open.
            }
        }

        private void AddTask_Click(object sender, RoutedEventArgs e)
        {
            //TODO
        }

        private void RemoveTask_Click(object sender, RoutedEventArgs e)
        {
            //TODO
        }


        protected internal override bool isContentOk()
        {
            Data.Deploy.Steps.Step step = DataContext as Data.Deploy.Steps.Step;
            if (step == null)
                return false;
            if (step.Description == null)
                return false;
            return true;
        }

        private void EditableTaskList_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            this.EditableTaskList.Environments = this.Environments;
            this.EditableTaskList.CurrentStep = this.DataContext as Data.Deploy.Steps.Step;
        }

        private void AbstractDialogContentControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            this.EditableTaskList.CurrentStep = this.DataContext as Data.Deploy.Steps.Step;
        }
    }
}
